******************************************************************************************************************************
**********************************Table 5*************************************************************************************
******************************************************************************************************************************



***********************************************welfare
use point,clear

forvalues i=1/14{
local falpha`i'=alpha`i'
}
local fbeta=0.5308357

keep impliedbeta1
append using flexlogit
drop if ID==.
drop impliedbeta1
clogit chosen x* z, group(ID)
 forvalues i=1/14 {
 local nalpha`i' = _b[x`i']
 }
local nbeta=_b[z]


gen xbeta0 = `nalpha1'*x1+`nalpha2'*x2+`nalpha3'*x3+`nalpha4'*x4+`nalpha5'*x5+`nalpha6'*x6+`nbeta'*z
gen xbeta1 = `falpha1'*x1+`falpha2'*x2+`falpha3'*x3+`falpha4'*x4+`falpha5'*x5+`falpha6'*x6+`fbeta'*z
forvalues i=7/14 {
replace xbeta0=xbeta0+`nalpha`i''*x`i'
replace xbeta1=xbeta1+`falpha`i''*x`i'
}
gen expxbeta0 = exp(xbeta0)
gen expxbeta1 = exp(xbeta1)
bys ID: egen sumexpxbeta0 = sum(expxbeta0)
bys ID: egen sumexpxbeta1 = sum(expxbeta1)
gen sij0 = expxbeta0/sumexpxbeta0
gen sij1=expxbeta1/sumexpxbeta1
bys ID: egen sumxbeta0 = sum(xbeta0*sij0)
bys ID: egen sumxbeta1 = sum(xbeta1*sij0)
*************************************************************************************************************************************************
***********************************************************Table 5 Welfare Difference per Choice ************************************************
*************************************************************************************************************************************************
gen DeltaCS = -(1/`falpha1')*(ln(sumexpxbeta1)-ln(sumexpxbeta0)+sumxbeta0-sumxbeta1)
sum DeltaCS, detail


*x1 price, x2 star, x3 review, x4 chian,x5 promotion, x6-14 position dummies

********************************************************************counterfactual***********************************************
*flexible 
*add back dropped position dummy 
gen x15=1
forvalues i=6/14{
	replace x15=x15-x`i'
}
forvalues i=1/15{
gen fx`i'=x`i'*sij1
bys ID:egen sumfx`i'=sum(fx`i') 
}

gen fz=z*sij1
bys ID:egen sumfz=sum(fz)
gen sumfpos=0

*aggregate position dummies
forvalues i=6/9{
sum sumfx`i'
replace sumfpos=sumfpos+r(mean)*(`i'-5)
}
forvalues i=10/14{
sum sumfx`i'
replace sumfpos=sumfpos+r(mean)*(`i'-4)
}
sum sumfx15
replace sumfpos=sumfpos+r(mean)*12
sum sumfpos 
rename (sumfx1 sumfx2 sumfx3 sumfx4 sumfx5 sumfz sumfpos)(price star review chain promotion location position)
*************************************************************************************************************************************************
**********************Table 5 Counterfactual column of average value of different attributes for the trasacted items in the data******************
*************************************************************************************************************************************************
sum price star review chain promotion position location 

rename (price star review chain promotion location position)(sumfx1 sumfx2 sumfx3 sumfx4 sumfx5 sumfz sumfpos)



*********************************************status quo******************************************************************************************
gen sumnpos=0

*aggregate position dummies
forvalues i=6/9{
sum x`i' if chosen==1
replace sumnpos=sumnpos+r(mean)*(`i'-5)
}
forvalues i=10/14{
sum x`i' if chosen==1
replace sumnpos=sumnpos+r(mean)*(`i'-4)
}
sum x15 if chosen==1
replace sumnpos=sumnpos+r(mean)*12
sum sumnpos 
rename (x1 x2 x3 x4 x5 z sumnpos)(price star review chain promotion location position)
*************************************************************************************************************************************************
**********************Table 5 Status quo column of average value of different attributes for the trasacted items in the data******************
*************************************************************************************************************************************************
sum price star review chain promotion position location if chosen==1



